﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Salon
{
    public partial class MainPage : Form
    {
        public MainPage()
        {
            InitializeComponent();
            this.IsMdiContainer = true;
        }

        private void MainPage_Load(object sender, EventArgs e)
        {
            
            Login login = new Login(this);
            login.ShowDialog();
            this.Hide();
            Global.MainPage = this;
        }

        /// <summary>
        /// Funkcja sprawdzająca czy formatka o danym typie jest już otwarta,
        /// jeśli jest to jest ona uwidaczniana
        /// </summary>
        /// <param name="formName"></param>
        public bool checkIfFormOpen(String formText)
        {
            foreach (Form form in this.MdiChildren)
            {
                if (form.Text.Equals(formText))
                {
                    form.Activate();
                    return true;
                }
            }

            return false;
        }

        /// <summary>
        /// Funkcja zweracająca czy formatkę o danej nazwie
        /// jeśli istnieje to jest ona zwracana 
        /// a jeśli nie to nie
        /// </summary>
        /// <param name="formName"></param>
        public Form getIfFormOpen(String formText)
        {
            foreach (Form form in this.MdiChildren)
            {
                if (form.Text.Equals(formText))
                {
                    return form;
                }
            }

            return null;
        }

       
        private void mojeDaneToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!checkIfFormOpen(Forms.USER_MY_DATA))
            {
                FormUser fu = new FormUser(Global.currentUser,true);
                fu.MdiParent = this;     
                fu.Show();

            }
        }

        private void nowyUżytkownikToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            openNewUserForm(null,false);
        }

        public void openNewUserForm(User user,bool current)
        {
            if (!checkIfFormOpen(Forms.USER_FORM))
            {
                FormUser fu = new FormUser(user,current);
                fu.MdiParent = this;
                fu.Show();
            }
        }

        private void rejestrUżytkownikówToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!checkIfFormOpen(Forms.USER_REGISTER))
            {
                RegisterUsers ru = new RegisterUsers();
                ru.MdiParent = this;
                ru.Text = Forms.USER_REGISTER;
                ru.Show();
            }
        }

        private void nowyKlientToolStripMenuItem_Click(object sender, EventArgs e)
        {
            openNewClientForm(null);
        }

        public void openNewClientForm(Client client)
        {
            if (!checkIfFormOpen(Forms.CLIENT_FORM))
            {
                FormClient fc = new FormClient(client);
                fc.MdiParent = this;
                fc.Show();
            }
        }

        private void rejestrKlientówToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!checkIfFormOpen(Forms.CLIENT_REGISTER))
            {
                RegisterClients rc = new RegisterClients();
                rc.MdiParent = this;
                rc.Text = Forms.CLIENT_REGISTER;
                rc.Show();
            }
        }

        private void nowySalonToolStripMenuItem_Click(object sender, EventArgs e)
        {
            openNewAutoSalonForm(null);

        }

        public void openNewAutoSalonForm(AutoSalon salon)
        {
            if (!checkIfFormOpen(Forms.AUTOSALON_FORM))
            {
                FormAutoSalon fc = new FormAutoSalon(salon);
                fc.MdiParent = this;
                fc.Show();
            }
        }

        private void rejestrSalonówToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!checkIfFormOpen(Forms.AUTOSALON_REGISTER))
            {
                RegisterAutoSalons rc = new RegisterAutoSalons();
                rc.MdiParent = this;
                rc.Text = Forms.AUTOSALON_REGISTER;
                rc.Show();
            }
        }
            
                
            
    }
}
